<?php
/**
 * Created by JetBrains PhpStorm.
 * User: T420
 * Date: 10/24/12
 * Time: 8:37 AM
 * To change this template use File | Settings | File Templates.
 */
class Controllers_Orders extends Libs_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->model=new Models_Orders();

    }
    public function index($param=null,$action=null)
    {
        $productperpage=10;
        $pagenum=$param[0];
        $this->view->action=$action;//lay ham
//        echo $action;
        $this->view->currenpage=$param[0];//trang hien tai
        $this->view->listorder=$this->model->getorder($pagenum,$productperpage,-1);
        $totalpage=ceil($this->model->totalpage/$productperpage);
        $this->view->totalpage=$totalpage;

        $this->view->render('orders/index');


    }
    public function neworder($param=null,$action)
    {
        //echo $action;
        $productperpage=10;
        $pagenum=$param[0];
        $this->view->action=$action;
        $this->action=$action;
        $this->view->listorder=$this->model->getorder($pagenum,$productperpage,0);
        $totalpage=ceil($this->model->totalpage/$productperpage);
        $this->view->totalpage=$totalpage;
        $this->view->render('orders/index');
        if($_POST['ItemDel']) {
            //$action='';
            if($_POST['element']=='') {

                echo '<script>alert(\'Mời bạn chọn ít nhất 1 bản tin\')</script>';
                return;
            }
            else{

                $id=implode(",",$_POST['element']);
                //echo $id;
                $this->model->delorder($id);


                echo '<script>location.reload();</script>';
            }
        }


    }
    public function shipments($param=null,$action)
    {
        $productperpage=10;
        $pagenum=$param[0];
        $this->view->action=$action;
        $this->view->listorder=$this->model->getorder($pagenum,$productperpage,1);
        $totalpage=ceil($this->model->totalpage/$productperpage);
        $this->view->totalpage=$totalpage;
        $this->view->render('orders/index');
    }
    public function invoices($param=null,$action)
    {
        $productperpage=2;
        $pagenum=$param[0];
        $this->view->action=$action;
        $this->view->listorder=$this->model->getorder($pagenum,$productperpage,2);
        $totalpage=ceil($this->model->totalpage/$productperpage);
        $this->view->totalpage=$totalpage;
        $this->view->render('orders/index');
    }
    public function detail($param=null)
    {
        $list=$this->model->getvieworder($param[0]);

        foreach($list as $key)
        {

            $output[]='
         <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">


        <tr>
        <td colspan="2" align="center" style="color:blue"><h2>Đơn hàng số '.$key->oid.'</h2></td>
        </tr>
        <tr>
        <td width="40%" bgcolor="#999999"><div align="center"><strong>Th&ocirc;ng tin &#273;&#7863;t h&agrave;ng </strong></div></td>
        <td width="60%" bgcolor="#999999"><div align="center"><strong>Th&ocirc;ng tin kh&aacute;ch h&agrave;ng </strong></div></td>
         </tr>';


            $output[]='<tr>
        <td>
        <form name="frmhoadonchitiet" method="post" action="" onsubmit="return fhoadonchitiet();" >
        <table width="274" border="1" cellpadding="0" cellspacing="0">
        <tr>
          <td width="162">Ng&agrave;y &#273;&#7863;t h&agrave;ng: </td>
          <td width="106">'.date('d-m-Y',strtotime($key->postdate)).'</td>
        </tr>
        <tr>
          <td>Ng&agrave;y giao h&agrave;ng: </td>
          <td>'.date('d-m-Y',strtotime($key->datereceipt)).'</td>
        </tr>
        <tr>
          <td>Tr&#7841;ng th&aacute;i: </td>
          <td>
            <select name="trangthaihd">';


		  	if($key->ostatus==0)
			{
	  	      $output[]='<option value="0">Chưa xử lý</option>
              <option value="1">Đang xử lý</option>
              <option value="2">Đã xử lý</option>';


			}
			else
				if($key->ostatus==1)
				{
 		     $output[]='<option value="0">Chưa xử lý</option>
              <option value="1" selected="selected">Đang xử lý</option>
              <option value="2">Đã xử lý</option>';
				}
				else if($key->ostatus==2)

				{
		    $output[]=' <option value="2">Đã xử lý</option>';
				}

            $output[]='</select> </td>
        </tr>';
            if($key->ostatus!=2)
            {
        $output[]='<tr><td colspan="2" align="center" class="button">
           <input type="submit" name="updatestt" value="Cập nhật trạng thái">
            </td></tr>';
            }
        $output[]='<tr>
          <td>H&igrave;nh th&#7913;c thanh to&aacute;n: </td>
          <td>'.$key->payname.'</td>
        </tr>
      <tr><td>Địa chỉ nhận hàng: </td><td>'.$key->readdress.'</td></tr>
      </table>
      </form>
      </td>
      <td valign="top"><table width="100%" border="1" cellpadding="0" cellspacing="0">
        <tr>
          <td width="96">H&#7885; t&ecirc;n: </td>
          <td width="187">'.$key->namecustomer.'</td>
        </tr>

        <tr>
          <td>&#272;i&#7879;n tho&#7841;i: </td>
          <td>'.$key->phone.'</td>
        </tr>
        <tr>
          <td>E-mail:  </td>
          <td>'.$key->email.'</td>
        </tr>

      </table></td>';


        $output[]='<tr>
      <td colspan="2">
       <form name="frmchitietdathang" method="post" action="'.PATH.'/administrator/orders/updateorder/'.$key->oid.'" onsubmit="return fchitietdathang();" >
      <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td height="33" colspan="5" align="center" bgcolor="#999999"><span class="style1">Sản phẩm đặt mua</span></td>
        </tr>
        <tr>
          <td width="9%" height="17" align="center"><strong>Số thứ tự</strong></td>
          <td width="28%" align="center"><strong>Tên sản phẩm</strong></td>
          <td width="19%" align="center"><strong>Giá (VND) </strong></td>
          <td width="15%" align="center"><strong>Số lượng</strong></td>
          <td width="29%" align="center"><strong>Thành tiền (VND) </strong></td>
        </tr>';
            $dem=0;
            $tong=0;
            $orderdetail=$this->model->getvieworderdetail($key->oid);
            foreach($orderdetail as $l=>$o)

                {
                    $pro=$this->model->getproduct($o->pid);
                    $output[]='<tr>
          <td align="center">'.++$dem.'</td>';
                    foreach($pro as $p)
                    {

                        $output[]='<td>'.$p->pname.' </td>';
                     }
          $output[]='<td align="right"><input type="text" name="price['.$o->odid.']" value="'.number_format($o->price,0,".",",").'" class="number"></td>
          <td align="right"><input type="text" name="qty['.$o->odid.']" value="'.number_format($o->quantity,0,",",".").'" class="number"></td>
          <td align="center">'.number_format($o->price*$o->quantity,0,",","."); $tong+=$o->price*$o->quantity.'</td>
        </tr>';



                }



          $output[]='<tr>
          <td colspan="4" align="right" style="border-right:0px">Tổng cộng (VND): </td>
          <td align="left" style="border-left:0px; color:#FF0000">'.number_format($tong,0,",",".").'</td>
        </tr>
      </table></td>';






        $output[]='</td></tr>';
        }


        $this->view->showorder=join('',$output);
        $this->view->showuser=$this->model->getvieworder($param[0]);
        $this->view->render('orders/detail');
        if($_POST['updatestt'])
        {
            $stt=$_POST['trangthaihd'];
            $employee=$_SESSION['userid'];
            $this->model->updatestt($employee,$stt,$param[0]);
           
            echo '<script type="text/javascript">alert("Cập nhật thành công!!!");location.reload();</script>';
        }
    }
    public function delete($param=null)

    {
       $this->model->delorder($param[0]);
        echo '<script type="text/javascript">history.back();</script>';
        //$this->index();
        //echo $this->action;
    }

    public function updateorder($param=null)
    {
        $oid=$param[0];
        if($_POST['sua'])
        {
            $qty=$_POST['qty'];
            $price=$_POST['price'];

            foreach($qty as $key=>$value)
            {
                //echo str_replace(",","",$price[$key]);
                $this->model->updateordertail($key,$value,str_replace(",","",$price[$key]));
            }
            echo '<script type="text/javascript">history.back();</script>';
        }
    }

}
